package cn.com.gridinfo.sooc.core.my.order.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.marshalchen.ultimaterecyclerview.UltimateRecyclerviewViewHolder;
import com.marshalchen.ultimaterecyclerview.UltimateViewAdapter;
import com.squareup.picasso.Picasso;

import java.util.List;

import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.core.my.order.bean.Order;
import cn.com.gridinfo.sooc.utils.DateUtil;

/**
 * 订单适配器
 * Created by yingchunli on 2016/8/10.
 */
public class OrderAdapter extends UltimateViewAdapter<OrderAdapter.ViewHolder> {


    public List<Order> orderList;
    Context context;

    public OrderAdapter(List<Order> orderList, Context context) {
        this.orderList = orderList;
        this.context = context;
    }

    public void loadmore(List<Order> list) {
        orderList.addAll(list);
        notifyDataSetChanged();
    }

    public void refresh(List<Order> list) {
        orderList.clear();
        orderList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        return null;
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {
    }

    @Override
    public ViewHolder getViewHolder(View view) {
        return new ViewHolder(view, false);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent) {
        //加载布局
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_order_item, parent, false);
        return new ViewHolder(view, true);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        if (position < getItemCount() &&
                (customHeaderView != null ? position <= orderList.size() : position < orderList.size())
                && (customHeaderView != null ? position > 0 : true)) {
            final Order order = orderList.get(position);

            holder.orderNo.setText("订单号: "+order.getOrderid());
            holder.kcls.setText(order.getKcjs());
            holder.kcmc.setText(order.getKcmc());
            holder.time.setText(DateUtil.datestamp2Date(order.getInput_time()));
            if (order.getStatus().equals("1")) {
                holder.orderStatus.setText("购买成功");
                holder.goPay.setText("查看课程");
                holder.cancelOrder.setVisibility(View.GONE);
            } else if (order.getStatus().equals("0")) {
                holder.orderStatus.setText("待付款");
            } else if(order.getStatus().equals("2")){
                holder.orderStatus.setText("已取消");
                holder.goPay.setVisibility(View.GONE);
                holder.cancelOrder.setVisibility(View.GONE);
            }
            Picasso.with(context).load(order.getKcfm()).resize(122, 73).into(holder.kcfm);
            holder.price.setText("￥" + order.getPrice());
            holder.lookCourse.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //进入课程学习页
                    mOnItemClickLitener.lookCourse(position,order);
                }
            });
            holder.cancelOrder.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //取消订单
                    mOnItemClickLitener.cancelOrder(position,order);
                }
            });
            holder.goPay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (holder.goPay.getText().toString().trim().equals("去支付")) {
                        //进入支付页面  传递kcid
                        mOnItemClickLitener.onGoPay(position,order);
                    } else {
                        //查看课程
                        mOnItemClickLitener.lookCourse(position,order);
                    }
                }
            });

        }
    }


    /**
     * 点击事件接口
     */
    public interface OnItemClickListener {
        //去支付
        void onGoPay(int postition, Order order);
        //查看课程
        void lookCourse(int position, Order order);
        //取消订单
        void cancelOrder(int position, Order order);
    }

    private OnItemClickListener mOnItemClickLitener;

    public void setmOnItemClickLitener(OnItemClickListener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    @Override
    public int getAdapterItemCount() {
        return orderList.size();
    }

    @Override
    public long generateHeaderId(int position) {
        return position;
    }

    public class ViewHolder extends UltimateRecyclerviewViewHolder {
        TextView orderNo, kcmc, price, kcls, orderStatus, time, goPay, cancelOrder;//订单号，课程名称，价钱，课程老师，订单状态,时间
        ImageView kcfm;//课程封面
        RelativeLayout lookCourse;//查看课程区域

        public ViewHolder(View itemView, boolean isItem) {
            super(itemView);
            if (isItem) {
                orderNo = (TextView) itemView.findViewById(R.id.orderNo);
                kcfm = (ImageView) itemView.findViewById(R.id.kcfm);
                kcmc = (TextView) itemView.findViewById(R.id.kcmc);
                price = (TextView) itemView.findViewById(R.id.price);
                kcls = (TextView) itemView.findViewById(R.id.kcls);
                orderStatus = (TextView) itemView.findViewById(R.id.orderStatus);
                time = (TextView) itemView.findViewById(R.id.time);
                lookCourse = (RelativeLayout) itemView.findViewById(R.id.look_course_rl);
                goPay = (TextView) itemView.findViewById(R.id.go_pay);
                cancelOrder = (TextView) itemView.findViewById(R.id.cancel_order);
            }
        }
    }
}
